package com.control.qct.businesssteward.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.adapter.DayStatisticAdapter;
import com.control.qct.businesssteward.adapter.DayStatisticTableAdapter;
import com.control.qct.businesssteward.adapter.GoodsStatisticAdapter;
import com.control.qct.businesssteward.base.BaseBean;
import com.control.qct.businesssteward.base.BaseFragment;
import com.control.qct.businesssteward.httputils.HttpUtils;
import com.control.qct.businesssteward.model.DayStatisticTableModel;
import com.control.qct.businesssteward.model.StoreModel;
import com.control.qct.businesssteward.utils.DateUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 日结统计表
 * Created by Administrator on 2017/12/25 0025.
 */

public class DayStatisticTableFragment extends BaseFragment implements HttpUtils.IHttpResult {
    @BindView(R.id.rb_cash_price)
    RadioButton mRbCashPrice;
    @BindView(R.id.rb_stroke_count)
    RadioButton mRbStrokeCount;
    @BindView(R.id.ll_day)
    LinearLayout mLlDay;
    @BindView(R.id.rg_day_statistic_table)
    RadioGroup mRgDayStatisticTable;
    @BindView(R.id.rv_statistical_table)
    RecyclerView mRvStatisticalTable;
    @BindView(R.id.rv_day_statistical)
    RecyclerView mRvDayStatistical;

    private int totalMax;
    private int countMax;
    private DayStatisticTableAdapter mDayStatisticTableAdapter;
    private DayStatisticAdapter mDayStatisticAdapter;

    private Map<String, String> mParams;

    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        return inflater.inflate(R.layout.fragment_day_statistic_table, container, false);
    }

    @Override
    protected void initView(View view) {
        activity.setTitle("日结统计表");
        mRbCashPrice.setChecked(true);
        //获取保存的门店实体类
        StoreModel storeModel = (StoreModel) mSPUtils.getObject(mNetProtocol.getUserCode() + "_store");
        getDayStatistic(DateUtil.toTime6(System.currentTimeMillis()), storeModel == null ? "" : storeModel
                .getStoreId());
        mRvStatisticalTable.setLayoutManager(new LinearLayoutManager(activity));
        mRvDayStatistical.setLayoutManager(new LinearLayoutManager(activity));
        mDayStatisticTableAdapter = new DayStatisticTableAdapter();
        mDayStatisticAdapter = new DayStatisticAdapter();
        mRvStatisticalTable.setAdapter(mDayStatisticTableAdapter);
        mRvDayStatistical.setAdapter(mDayStatisticAdapter);
        mRgDayStatisticTable.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
                switch (checkId) {
                    case R.id.rb_cash_price:
                        mDayStatisticTableAdapter.setProgressMax((int) Math.ceil(totalMax * 1.1), 1);
                        break;
                    case R.id.rb_stroke_count:
                        mDayStatisticTableAdapter.setProgressMax((int) Math.ceil(countMax * 1.1), 2);
                        break;
                }
            }
        });
    }

    /**
     * 获取日结统计表
     */
    public void getDayStatistic(String date, String storeId) {
        mParams = new HashMap<>();
        mParams.put("date", date);
        mParams.put("storeId", storeId);
        mNetProtocol.getDayStatistic(activity, mParams, this);

    }

    @Override
    public void onSuccess(BaseBean baseBean) {
        totalMax = 0;
        countMax = 0;
        mRbCashPrice.setChecked(true);
        DayStatisticTableModel data = (DayStatisticTableModel) baseBean.getData();
        mLlDay.setVisibility(data.getDetails().size() == 0 ? View.GONE : View.VISIBLE);
        for (int i = 0; i < data.getSummary().size(); i++) {
            if (totalMax < data.getSummary().get(i).getTotal()) {
                totalMax = data.getSummary().get(i).getTotal();
            }
            if (countMax < data.getSummary().get(i).getCount()) {
                countMax = data.getSummary().get(i).getCount();
            }
        }
        mDayStatisticAdapter.setNewData(data.getDetails());
        mDayStatisticTableAdapter.setNewData(data.getSummary());
        mDayStatisticTableAdapter.setProgressMax((int) Math.ceil(totalMax * 1.1), 1);
    }


    @Override
    public void onFail(BaseBean baseBean) {
        showShortToast(baseBean.getMessage());
    }
}
